package demo;

import javax.persistence.*;

import org.mortbay.log.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Geofence extends HttpServlet{
	private static final Logger log = Logger.getLogger(MyTrackServlet.class
			.getName());
	EntityManager em;
	    
	    private String imei;
	    private String distance;
	    private String latitude;
	    private String longitude;
	    private String fenceType;
	    private String name;
	    
	    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
	        //MyClass myClass = new MyClass();
	    	imei = request.getSession().getAttribute("imei").toString();

	        if (request.getParameter("distance") != null) {
	        	distance = request.getParameter("distance");
	        }
	        if (request.getParameter("latitude") != null) {
	        	latitude = request.getParameter("latitude");
	        }
	        if (request.getParameter("longitude") != null) {
	        	longitude = request.getParameter("longitude");
	        }
	        if (request.getParameter("fenceType") != null) {
	        	fenceType = request.getParameter("fenceType");
	        }
	        if (request.getParameter("name") != null) {
	        	name = request.getParameter("name");
	        }
	        
	        String PermanentType = "Permanent";
	        String TemporaryType = "Temporary";
	        if (fenceType.equals(PermanentType)){
	        	DeletePermanentFenceData();
	        	addPermanentFenceData(imei,distance,latitude,longitude,name);
	        }
	        
	        if (fenceType.equals(TemporaryType)){
	        	DeleteTemporaryFenceData();
	        	addTemporaryFenceData(imei,distance,latitude,longitude,name);
	        }
	        
			PrintWriter out = response.getWriter();
	        out.println("<html><body onLoad=\"alert('Geofence Updated');window.location.href='http://omnihtrack.appspot.com/index.html';\"></html>");
	        
    		//response.setContentType("text/plain");
    		//response.getWriter().println("OK" + " " + imei + " " + distance + " " + latitude + " " + longitude);

    	}
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws IOException {
			doGet(request,response);
		}
		
		public boolean addTemporaryFenceData(String imei, String distance, String latitude, String longitude, String name) {
			TemporaryGeofenceData geofence = new TemporaryGeofenceData();
			em = EMF.get().createEntityManager();
			geofence.setImei(imei);
			geofence.setdistance(distance);
			geofence.setlatitude(latitude);
			geofence.setlongitude(longitude);
			geofence.setCurrent(new Date());
			geofence.setName(name);
			em.persist(geofence);
			em.close();
			return true;
		}
		

		public void DeleteTemporaryFenceData() {

			//Date subdate = getCurre();
			
			em = EMF.get().createEntityManager();
			
			Query q = em.createQuery("delete from TemporaryGeofenceData d where imei = '" + imei + "'");
			//q.setParameter("subdate", subdate);
			int i = q.executeUpdate();
			//System.out.println("Deleted: " + i);
			em.close();
		}
		
		public boolean addPermanentFenceData(String imei, String distance, String latitude, String longitude, String name) {
			PermanentGeofenceData geofence = new PermanentGeofenceData();
			em = EMF.get().createEntityManager();
			geofence.setImei(imei);
			geofence.setdistance(distance);
			geofence.setlatitude(latitude);
			geofence.setlongitude(longitude);
			geofence.setCurrent(new Date());
			geofence.setName(name);
			em.persist(geofence);
			em.close();
			return true;
		}
		

		public void DeletePermanentFenceData() {

			//Date subdate = getCurre();
			
			em = EMF.get().createEntityManager();
			
			Query q = em.createQuery("delete from PermanentGeofenceData d where imei = '" + imei + "'");
			//q.setParameter("subdate", subdate);
			int i = q.executeUpdate();
			//System.out.println("Deleted: " + i);
			em.close();
		}

	    



}
